//
//  TitleButton.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/4/20.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit

class TitleButton: UIButton {

   override init(frame: CGRect) {
       super.init(frame: frame)
    titleLabel?.font = UIFont.systemFontOfSize(16)
    setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
    setImage(UIImage(named: "navigationbar_arrow_up"), forState: UIControlState.Selected)
    setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("ss dd d f f d")
    }
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        if titleLabel?.frame.origin.x > imageView?.frame.origin.x {
            titleLabel?.frame.origin.x =  imageView!.frame.origin.x
        }
        imageView?.frame.origin.x =  CGRectGetMaxX(titleLabel!.frame) + 4
    }
    
    
    override func setTitle(title: String?, forState state: UIControlState) {
       super.setTitle(title, forState: state)
        sizeToFit()
    }
    
    
    override func setImage(image: UIImage?, forState state: UIControlState) {
        super.setImage(image, forState: state)
        sizeToFit()
    }
    
    
}
